<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 教室管理逻辑
 */
class Classroom extends AdminBase
{
    
    /**
     * 获取教室信息列表
     */
    public function getClassroomList($where = [], $field = true, $order = '', $paginate = 0,$a='')
    {
        
        $order='id';
        $this->modelClassroom->alias('c');
        $join=[['subject b', 'b.adress = c.name','left']];
        $this->modelClassroom->join=$join;
        $field='c.*,b.subject as bname';
        return $this->modelClassroom->getList($where, $field, $order,$paginate,$a);
    }
    
    /**
     * 获取课程信息列表
     */
   public function getSubjectList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        $order='id';
        $this->modelClassroom->alias('c');
        $join=[['subject b', 'b.adress = c.name','left']];
        $this->modelClassroom->join=$join;
        $field='c.*,b.subject as bname';
        return $this->modelClassroom->getList($where, $field, $order,$paginate,$a);
    }
    
    /**
     * 获取会员列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['name'] = ['like', '%'.$data['search_data'].'%'];
        
        if (!is_administrator()) {
            
            $member = session('member_info');
            
            if ($member['is_share_member']) {
                
                $ids = $this->getInheritMemberIds(MEMBER_ID);
                
                $ids[] = MEMBER_ID;
                
                $where['m.leader_id'] = ['in', $ids];
                
            } else {
                
                $where['m.leader_id'] = MEMBER_ID;
            }
        }
        
        return $where;
    }

    /**
     * 友情链接信息编辑
     */
    public function roomEdit($data = [])
    {
        
        $validate_result = $this->validateClassroom->scene('edit')->check($data);
        
        if (!$validate_result) : return [RESULT_ERROR, $this->validateClassroom->getError()]; endif;
        
        $url = url('roomlist');
        
        $result = $this->modelClassroom->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '友情链接' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelClassroom->getError()];
    }

    /**
     * 获取友情链接信息
     */
    public function getClassroomInfo($where = [], $field = true)
    {
        
        return $this->modelClassroom->getInfo($where, $field);
    }
    
    /**
     * 友情链接删除
     */
    public function roomDel($where = [])
    {
        
        $result = $this->modelClassroom->deleteInfo($where);
        
        $result && action_log('删除', '友情链接删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelClassroom->getError()];
    }
}
